package com.cell.api.application.netty.server;

import com.cell.api.application.netty.coder.PacketDecoder;
import com.cell.api.application.netty.coder.PacketEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * <p>
 * netty服务初始化器
 * </p>
 *
 * @author zdy
 * @date 2021/11/6
 */
@Component
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Resource
    private NettyServerHandler nettyServerHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //添加编解码
        socketChannel.pipeline().addLast(new PacketDecoder());
        socketChannel.pipeline().addLast(new PacketEncoder());
        //自定义的空闲检测 1分钟检测读空闲 2分钟检测写空闲  5分钟检测读写空闲
        socketChannel.pipeline().addLast(new IdleStateHandler(60, 2 * 60, 5 * 60));
        socketChannel.pipeline().addLast(nettyServerHandler);
    }
}
